package yl.hs.bmipfull.controllers.construction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.production.IFinishProductInventoryDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.service.finance.IContractAccountService;
import yl.hs.bmipfull.service.production.IFinishProductInventoryService;
import yl.hs.bmipfull.service.salemanage.ISaleContractProductsService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * ConstructionContractController 工地合同信息
 * @category 销售管理
 * @author 谢景广
 * @date 2022-04-21
 */
@RequestMapping("/construction/contract")
@Api(value = "工地系统", tags = "工地系统——合同管理")
@RestController
public class ConstructionContractController extends ControllerBase {

    @Autowired
    IContractAccountService contractAccountService;
    @Autowired
    ISaleContractProductsService productsService;
    @Autowired
    IFinishProductInventoryService finishservice;
    @PostMapping("account/{rowNum}/{page}")
    @ApiOperation(value = "查询合同台帐信息", notes = "分页查询合同台帐信息" )
    public WebResult queryPage(@ApiParam(value = "查询") @RequestBody PageQuery query, @PathVariable Integer page, @PathVariable Integer rowNum){
        try {
            var list = contractAccountService.queryHfListByGD(query, page, rowNum);
            return success("ok",list);
        } catch (Exception e) {
            return error(e);
        }
    }

    @PostMapping("query/{rowNum}/{page}")
    @ApiOperation(value = "查询用户合同", notes = "分页查询客户合同信息" )
    public WebResult queryContractList(@PathVariable("rowNum")int rowNum,@PathVariable("page")int page,@RequestBody PageQuery query){
        try {
            var result = contractService.queryCustomerContracts(query,rowNum,page);
            return success("OK", result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @PostMapping("query/salecontractproduct")
    @ApiOperation(value = "查询用户合同", notes = "分页查询客户合同信息" )
    public WebResult saleContractProduct(){
        try {
            PageQuery page = new PageQuery();
            List<Object> data = new ArrayList<>();
            var result = contractService.queryCustomerContracts(page,999,1);
            if(result.getData()!=null){
                var contract = result.getData();
                for (Map<String, Object> map : contract) {
                   var ss = productsService.queryBySaleBSn(map.get("sn").toString());
                   ss.forEach(d->{
                       var finish = finishservice.quantityByProductSN(d.getProductSN());
                       if(finish!=null && finish.size()>0){
                           data.addAll(finish);
                       }
                   });
                }
            }
            return success("OK", data.stream().distinct().collect(Collectors.toList()));
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @RequestMapping(value = "count",method = RequestMethod.GET)
    @ApiOperation(value = "查询用户合同", notes = "分页查询客户合同信息" )
    public WebResult queryCount(){
        try {
            var result = contractService.queryCustomerContracts(new PageQuery(),10,1);
            return success("OK", result.getRecords());
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "查询销售合同信息",notes = "根据销售合同信息编号查询销售合同信息")
    @RequestMapping(value = "/info/{sn}",method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn){
        return success("查询成功",contractService.findBySn(sn));
    }
    @Autowired
    ISaleContractService contractService;
}
